package com.chen.jwt.config;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @Author xlj
 * @Date Created in  2023/2/15 16:42
 * @DESCRIPTION:
 * @Version V1.0
 */
@EnableCaching
@Configuration
public class redisConfig extends CachingConfigurerSupport {

   @Bean
   public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
      RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
      redisTemplate.setConnectionFactory(redisConnectionFactory);

      StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
      // key采用 String的序列化方式
      redisTemplate.setKeySerializer(stringRedisSerializer);
      // hash的 key也采用 String的序列化方式
      redisTemplate.setHashKeySerializer(stringRedisSerializer);
      // value序列化方式采用 jdk
      redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
      // hash的 value序列化方式采用 jdk
      redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
      redisTemplate.afterPropertiesSet();
      return redisTemplate;
   }

}
